package aes.client;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

import javax.swing.*;
import javax.swing.border.*;

import common.*;

public class MainUI extends JPanel implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1154000723005602560L;

	private ClientUI ui;
	private Client c;
	private JButton btnQuestions;
	private JButton btnExams;
	private JButton btnExecuteExam;
	private JButton btnReports;
	private JButton btnExit;
	private JPanel panel_1;
	private JPanel panel_2;
	private JLabel lblWelcome;
	
	/**
	 * Constructor
	 * @param c Client object reference
	 * @param ui ClientUI object reference
	 */
	public MainUI (Client c, ClientUI ui) {
		this.c = c;
		this.ui = ui;
		JFrame frame = new JFrame();
		frame.setSize(640, 480);
		frame.add(this);
		
		setLayout(new GridBagLayout());

		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		panel.setPreferredSize(new Dimension(600,400));
		panel.setSize(new Dimension(600,400));
		
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(5, 0, 5, 0);
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 0;
		add(panel,gbc_panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 1.0};
		gbl_panel.columnWeights = new double[]{1.0, 0.0, 0.0};
		panel.setLayout(gbl_panel);
		
		JLabel lblAesTitle = new JLabel("AES Main Menu");
		lblAesTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lblAesTitle.setVerticalAlignment(SwingConstants.TOP);
		lblAesTitle.setBounds(7, 10, 70, 17);
		lblAesTitle.setFont(new Font("Tahoma", Font.BOLD, 14));
		
		GridBagConstraints gbc_lblAesTitle = new GridBagConstraints();
		gbc_lblAesTitle.gridwidth = 3;
		gbc_lblAesTitle.fill = GridBagConstraints.BOTH;
		gbc_lblAesTitle.insets = new Insets(5, 0, 25, 0);
		gbc_lblAesTitle.gridx = 0;
		gbc_lblAesTitle.gridy = 0;
		panel.add(lblAesTitle, gbc_lblAesTitle);
		
		lblWelcome = new JLabel("");
		GridBagConstraints gbc_lblWelcome = new GridBagConstraints();
		gbc_lblWelcome.insets = new Insets(0, 0, 5, 5);
		gbc_lblWelcome.gridx = 0;
		gbc_lblWelcome.gridy = 1;
		panel.add(lblWelcome, gbc_lblWelcome);

		panel_1 = new JPanel();
		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
		gbc_panel_1.insets = new Insets(30, 0, 0, 0);
		gbc_panel_1.fill = GridBagConstraints.BOTH;
		gbc_panel_1.gridx = 0;
		gbc_panel_1.gridy = 2;
		gbc_panel_1.gridwidth = 3;
		panel.add(panel_1, gbc_panel_1);
		panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		btnQuestions = new JButton("Questions", new ImageIcon(MainUI.class.getResource("/aes/client/images/question.png")));
		btnQuestions.addActionListener(this); 
		GridBagConstraints gbc_btnQuestions = new GridBagConstraints();
		gbc_btnQuestions.insets = new Insets(5, 5, 5, 5);
		panel_1.add(btnQuestions, gbc_btnQuestions);
		btnQuestions.setBounds(353, 7, 57, 23);
		
		btnExams = new JButton("Exams", new ImageIcon(MainUI.class.getResource("/aes/client/images/Exam.png")));
		btnExams.addActionListener(this); 
		GridBagConstraints gbc_btnExams = new GridBagConstraints();
		gbc_btnExams.insets = new Insets(5, 5, 5, 5);
		panel_1.add(btnExams, gbc_btnExams);
		btnExams.setBounds(353, 7, 57, 23);
		
		btnExecuteExam  = new JButton("Execute Exam", new ImageIcon(MainUI.class.getResource("/aes/client/images/execute.png")));
		btnExecuteExam.addActionListener(this); 
		GridBagConstraints gbc_btnExecuteExam = new GridBagConstraints();
		gbc_btnExecuteExam.insets = new Insets(5, 5, 0, 5);
		panel_1.add(btnExecuteExam, gbc_btnExecuteExam);
		btnExecuteExam.setBounds(353, 7, 57, 23);
		
		btnReports = new JButton("Reports", new ImageIcon(MainUI.class.getResource("/aes/client/images/Report.png")));
		btnReports.addActionListener(this); 
		GridBagConstraints gbc_btnReports = new GridBagConstraints();
		gbc_btnReports.insets = new Insets(5, 5, 0, 5);
		panel_1.add(btnReports, gbc_btnReports);
		btnReports.setBounds(353, 7, 57, 23);
		
		panel_2 = new JPanel();
		GridBagConstraints gbc_panel_2 = new GridBagConstraints();
		gbc_panel_2.insets = new Insets(30, 0, 0, 0);
		gbc_panel_2.fill = GridBagConstraints.BOTH;
		gbc_panel_2.gridx = 0;
		gbc_panel_2.gridy = 4;
		gbc_panel_2.gridwidth = 3;
		panel.add(panel_2, gbc_panel_2);
		panel_2.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
		
		btnExit = new JButton("Logout", new ImageIcon(MainUI.class.getResource("/aes/client/images/LogOut.png")));
		btnExit.addActionListener(this); 
		GridBagConstraints gbc_btnExit = new GridBagConstraints();
		gbc_btnExit.insets = new Insets(5, 5, 0, 5);
		panel_2.add(btnExit, gbc_btnExit);
		btnExit.setBounds(353, 7, 57, 23);
		btnExams.setVisible((ui.getUser() instanceof Teacher) || (ui.getUser() instanceof Principal));
		btnQuestions.setVisible((ui.getUser() instanceof Teacher) || (ui.getUser() instanceof Principal));
		
		// Set welcome message.
		if (ui.getUser() != null) {
			User u = ui.getUser();
			String first_name = (u.getFirstname() != null)? u.getFirstname() : "";
			String last_name = (u.getLastname() != null)? u.getLastname() : "";
			String role = "Principal";
			if (u instanceof Teacher)
				role = "Teacher";
			else if (u instanceof Student)
				role = "Student";
			lblWelcome.setText("Welcome " + first_name + " " + last_name + " (" + role + ")");
		}
	}

	@SuppressWarnings("static-access")
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(btnExit)) {
			// Logout.
			CommandMessage msg = new CommandMessage(CommandMessage.LOGOUT_USER,new String[] {Integer.toString(ui.getUser().getId())});
			try {
				c.sendToServer(msg);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		else if (e.getSource().equals(btnQuestions)) {
			ui.SetContent(ui.QUESTIONS_MANAGER_SCREEN);
		}
		else if (e.getSource().equals(btnExams)) {
			ui.SetContent(ui.EXAMS_MANAGER_SCREEN);
		}
		else if (e.getSource().equals(btnExecuteExam)) {
			User u = ui.getUser();
			if (u instanceof Teacher)
				ui.SetContent(ui.TEACHER_EXAM_EXECUTION_SCREEN);
			else if (u instanceof Student)
				ui.SetContent(ui.STUDENT_EXAM_EXECUTION_SCREEN);
			else if (u instanceof Principal)
				ui.SetContent(ui.PRINCIPAL_EXAM_EXECUTION_SCREEN);
				
		}
		else if (e.getSource().equals(btnReports)) {
			User u = ui.getUser();
			if (u instanceof Student)
				ui.SetContent(ui.STUDENT_REPORT_SCREEN);
			else if (u instanceof Teacher)
				ui.SetContent(ui.TEACHER_REPORT_SCREEN);
			else if (u instanceof Principal)
				ui.SetContent(ui.PRINCIPAL_REPORTS_SCREEN);
		}
	}
}
